﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;

namespace BLL.NguoiDung
{
    public class NguoiDung
    {
        public static void ThemNguoiDung(string userName, bool sex,DateTime birth, string relationShip,string email,string matKhau,string dienthoai,string didong,string urlAvatar,string diaChi,int tinhId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            db.Users.InsertOnSubmit( new User
                                         {
                                            UserName = userName,
                                            Sex = sex,
                                            Birth = birth,
                                            RelationShip = relationShip,
                                            Email = email,
                                            MatKhau = Tool.MaHoa(matKhau),
                                            DiDong = didong,
                                            DienThoai = dienthoai,
                                            NgayDangKy = DateTime.Now,
                                            ChuoiKichHoat = Tool.MaNgauNhien_SoChu(64),
                                            DaKichHoat = false,
                                            UrlAvatar = urlAvatar,
                                            DiaChi = diaChi,
                                            TinhId = tinhId
                                         });
            db.SubmitChanges();
        }

        public static void UpdateNguoiDung(int userId,string userName, bool sex,DateTime birth, string relationShip,string dienthoai,string didong, string urlAvatar,string diaChi, int tinhId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var user = db.Users.FirstOrDefault(r => r.UserId == userId);
            if(user!=null)
            {
                user.UserName = userName;
                user.Sex = sex;
                user.Birth = birth;
                user.RelationShip = relationShip;
                user.DienThoai = dienthoai;
                user.DiDong = didong;
                if (urlAvatar != null)
                    user.UrlAvatar = urlAvatar;
                user.DiaChi = diaChi;
                user.TinhId = tinhId;
                db.SubmitChanges();
            }
            
        }
        public static void DeleteNguoiDung(int userId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var user = db.Users.FirstOrDefault(r => r.UserId == userId);
            if (user != null)
            {
                db.Users.DeleteOnSubmit(user);
                db.SubmitChanges();
            }
        }

        public static bool CheckEmail(string email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            if(db.Users.Any(r=>r.Email==email))
            {
                return false;
            }
            return true;
        }

        public static int ChangePass(int userId,string passOld,string passNew)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var user = db.Users.FirstOrDefault(r => r.UserId == userId);
            if(user!=null)
            {
                if(Tool.MaHoa(passOld)==user.MatKhau)
                {
                    user.MatKhau = Tool.MaHoa(passNew);
                    db.SubmitChanges();
                    return 1;
                }
                return -1;
            }
            return -1;
        }

        public static bool Active(string email,string activeCode)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var user = db.Users.FirstOrDefault(r => r.Email == email);
            if (user != null)
            {
                if(user.ChuoiKichHoat==activeCode)
                {
                    user.DaKichHoat = true;
                    db.SubmitChanges();
                    return true;
                }
            }
            return false;
        }

        public static User Get_UserEmail(string email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("StrConnect");
            return db.Users.FirstOrDefault(r => r.Email == email);
        }

        public static bool Login(string email,string matkhau)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("StrConnect");
            var user = db.Users.FirstOrDefault(r => r.Email == email && r.MatKhau == Tool.MaHoa(matkhau));
            if (user != null)
                return true;
            return false;
        }
    }
}